// pages/detail/detail.js
const app = getApp()
const db = wx.cloud.database({
  env: 'nodejs-zyvej'
})
const _ = db.command



Page({
  /**
   * 页面的初始数据
   */
  data: {
    concertId: "",
    temp: [],
    concertInfo: [],
    concerts: [],
    isCollect: "",
    collectedList: [],
    list: [],
    hadBought: {
      // "0d3f11e5-b401-4a85-98b8-bf592c54ddff":0
    }
  },
  // buy: function () {
  //   wx.navigateTo({
  //     url: '../buy/buy',
  //   })
  // },

  hasCollect() {
    let temp = 1;
    this.setData({
      isCollect: temp
    })
    this.data.collectedList.push(this.data.concertId);
    console.log(this.data.collectedList)
    var myConcertId = this.data.concertId;
    wx.cloud.callFunction({
      name: 'add_collect',
      data: {
        openid: app.globalData.openid,
        concertId: myConcertId
      },
      success: res => {
        console.log('success')
        // this.data.isCollect = 1
      },
      fail: err => {
        console.log('fail')
        console.error(err)
      }
    })
  },

  cancelCollect() {
    let temp = 0;
    this.setData({
      isCollect: temp
    })
    var myConcertId = this.data.concertId;
    wx.cloud.init()
    wx.cloud.callFunction({
      name: 'del_collect',
      data: {
        openid: app.globalData.openid,
        concertId: myConcertId
      },
      success: res => {
        // this.data.isCollect = 0
      },
      fail: err => {
        console.error(err)
      }
    })

  },



  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    console.log(options)
    wx.cloud.init()
    this.data.concertId = options.concertId
    console.log("this.data.concertId:" + this.data.concertId)
    var tempconcertId = options.concertId
    var tempId = options.tempConcert;
    console.log(tempId);
    this.setData({
      temp: tempId,
      concertId: tempconcertId,
      isCollect: 0
    });

    db.collection('concert_image').where({
      concertId: this.data.concertId
    }).get({
      success: res => {
        console.log(res.data)
        var a = res.data.length;
        var k=[]
        for(var i=0;i<a;i++){
          k.push(res.data[i].img_url);
        }
        this.setData({
          concertInfo:k
        })
        console.log(this.data.concertInfo)
      }
    })

    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        this.openid = res.result.openid
        wx.cloud.callFunction({
          name: 'Get_Tickets',
          data: {
            userid: this.openid,
            concertid: []
          },
          success: res => {
            console.log(res)
            const ticketsdata = res.result.data
            const that = this
            for (var i = 0; i < ticketsdata.length; i++) {
              that.data.hadBought[ticketsdata[i].concertid] = 1
            }
            console.log(that.data.hadBought)
            if (that.data.hadBought[this.data.concertId] != 1) {
              that.data.hadBought[this.data.concertId] = -1
            }
            app.globalData.hadBought = that.data.hadBought
            this.setData({
              hadBought: that.data.hadBought
            })
          },
          fail: err => {

          }
        })
      },
      fail: err => {
        console.error(err)
      }
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var myConcertId = this.data.concertId;
    console.log("app.globalData.hadBought[this.data.concertId]:"+app.globalData.hadBought[this.data.concertId])
    if (app.globalData.hadBought[this.data.concertId] == 1){
      const that = this
      that.data.hadBought[this.data.concertId] = 1
      this.setData({
        hadBought: that.data.hadBought
      })
    }

    console.log("this.data.concertId:" + this.data.concertId)
    wx.cloud.init()
    wx.cloud.callFunction({
      name: 'searchConcertById',
      data: {
        openid: app.globalData.userInfo.openid,
        concertId: myConcertId
      },
      success: res => {
        console.log(res)
       var len = res.result.data.length
       if(len == 0){
         this.setData({
           isCollect:0
         })
       }
       else{
         this.setData({
           isCollect:1
         })
       }
      },
      fail: err => {
        console.error(err)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})